//获取应用实例
const app = getApp()

Page({
  data: {
    listHeight: Number,
    indicatorDots: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    circular: true,
    marqueeText: [],
    marqueePace: 1, //滚动速度
    marqueeDistance: 0, //初始滚动距离
    size: 14,
    orientation: 'left', //滚动方向
    marqueeInterval: 20, // 时间间隔
    bannerList: [], // 轮播图
    tabList: [], // 四大模块
    moduleList: [] //模块大图加小图
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    let textLength = ''
    this.data.marqueeText.map((item, index) => textLength += item.title)
    var length = textLength.length * this.data.size //文字长度
    var windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕宽度
    this.setData({
      length: length,
      windowWidth: windowWidth
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // 登录
    wx.login({
      success: res => {
        const url = 'https://www.bisaiquan.cn/api/wechat/login'
        const data = {
          code: res.code
        }
        wx.request({
          url: url,
          data: data,
          method: 'POST',
          success(response) {
            wx.setStorageSync('openToken', response.data.data.open_token)
          }
        })
      }
    })

    const _this = this
    let query = wx.createSelectorQuery()
    query.select('#indexTop').boundingClientRect()
    query.exec(function (resel) {
      wx.getSystemInfo({
        success: (res) => {
          _this.setData({
            listHeight: res.windowHeight - resel[0].height
          })
        },
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getIndexDatas()
  },
  // 返回slides、notices、modules、menus四个模块的数据
  getIndexDatas() {
    const _this = this
    const url = 'content/index'
    const data = {}
    app.$http.post(url, data).then(res => {
      console.log(res.data.modules)
      _this.setData({
        bannerList: res.data.slides,
        marqueeText: res.data.notices,
        tabList: res.data.modules,
        moduleList: res.data.menus
      })
    })
  },
  // 轮播图入口
  bannerNavTo: function (ev) {
    const type = ev.currentTarget.dataset.type
    const to = ev.currentTarget.dataset.to
    const url = '/pages/article/article'
    const outlink = '/pages/outlink/outlink'
    if (type === 'thread') {
      app.pageNavigateTo(`${url}?wthread_id=${to}`)
    } else {
      wx.setStorage({
        key: 'eventUrl',
        data: to
      })
      app.pageNavigateTo(outlink)
    }
  },
  // 大模块进入列表页 - ipage图文单页、events活动列表、ilist主题列表
  pageNavigateTo: function (ev) {
    const id = ev.currentTarget.dataset.menu_id
    const type = ev.currentTarget.dataset.menu_type
    const linkUrl = ev.currentTarget.dataset.out_link
    const name = ev.currentTarget.dataset.menu_name
    if (type === 'ilist') {
      const url = `/pages/ilist/ilist?menu_id=${id}&menu_name=${name}`
      // app.pageNavigateTo(url)
      wx.redirectTo({
        url: url,
      })
    } else if (type === 'events') {
      const url = `/pages/events/events?menu_id=${id}&menu_name=${name}`
      // app.pageNavigateTo(url)
      wx.redirectTo({
        url: url,
      })
    } else if (type === 'link') {
      const outlink = '/pages/outlink/outlink'
      wx.setStorage({
        key: 'eventUrl',
        data: linkUrl
      })
      app.pageNavigateTo(outlink)
    } else {
      // ipage
      const url = `/pages/ipage/ipage?menu_id=${id}`
      app.pageNavigateTo(url)
    }

  }
})